{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the necessary modules\n",
    "import sys, pygame\n",
    "\n",
    "# Initialize Pygame\n",
    "pygame.init()\n",
    "\n",
    "# Set up the screen size and ball speed\n",
    "size = width, height = 320, 240\n",
    "speed = [2, 2]\n",
    "\n",
    "# Define colors\n",
    "black = 0, 0, 0\n",
    "\n",
    "# Create a window on the screen\n",
    "screen = pygame.display.set_mode(size)\n",
    "\n",
    "# Load the image of the ball and create a rectangle object around it\n",
    "ball = pygame.image.load(\"intro_ball.gif\")\n",
    "ballrect = ball.get_rect()\n",
    "\n",
    "# Start the main loop of the program\n",
    "while True:\n",
    "    # Check for events and exit if the user clicks the close button\n",
    "    for event in pygame.event.get():\n",
    "        if event.type == pygame.QUIT: \n",
    "            sys.exit()\n",
    "\n",
    "    # Move the ball based on its speed and direction\n",
    "    ballrect = ballrect.move(speed)\n",
    "\n",
    "    # If the ball hits the edge of the screen, reverse its direction\n",
    "    if ballrect.left < 0 or ballrect.right > width:\n",
    "        speed[0] = -speed[0]\n",
    "    if ballrect.top < 0 or ballrect.bottom > height:\n",
    "        speed[1] = -speed[1]\n",
    "\n",
    "    # Clear the screen and draw the ball at its new position\n",
    "    screen.fill(black)\n",
    "    screen.blit(ball, ballrect)\n",
    "\n",
    "    # Update the display\n",
    "    pygame.display.flip()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is as simple as you can get for a bouncing animation. First we see importing and initializing pygame is nothing noteworthy. The import pygame imports the package with all the available pygame modules. The call to pygame.init() initializes each of these modules.\n",
    "\n",
    "On line 8 we create a graphical window with the call to pygame.display.set_mode(). Pygame and SDL make this easy by defaulting to the best graphics modes for the graphics hardware. You can override the mode and SDL will compensate for anything the hardware cannot do. Pygame represents images as Surface objects. The display.set_mode() function creates a new Surface object that represents the actual displayed graphics. Any drawing you do to this Surface will become visible on the monitor.\n",
    "\n",
    "At line 10 we load our ball image. Pygame supports a variety of image formats through the SDL_image library, including BMP, JPG, PNG, TGA, and GIF. The pygame.image.load() function returns us a Surface with the ball data. The Surface will keep any colorkey or alpha transparency from the file. After loading the ball image we create a variable named ballrect. Pygame comes with a convenient utility object type named Rect, which represents a rectangular area. Later, in the animation part of the code, we will see what the Rect objects can do.\n",
    "\n",
    "At this point, line 13, our program is initialized and ready to run. Inside an infinite loop we check for user input, move the ball, and then draw the ball. If you are familiar with GUI programming, you have had experience with events and event loops. In pygame this is no different, we check if a QUIT event has happened. If so we simply exit the program, pygame will ensure everything is cleanly shutdown.\n",
    "\n",
    "It is time to update our position for the ball. Lines 17 moves the ballrect variable by the current speed. Lines 18 thru 21 reverse the speed if the ball has moved outside the screen. Not exactly Newtonian physics, but it is all we need.\n",
    "\n",
    "On line 23 we erase the screen by filling it with a black RGB color. If you have never worked with animations this may seem strange. You may be asking \"Why do we need to erase anything, why don't we just move the ball on the screen?\" That is not quite the way computer animation works. Animation is nothing more than a series of single images, which when displayed in sequence do a very good job of fooling the human eye into seeing motion. The screen is just a single image that the user sees. If we did not take the time to erase the ball from the screen, we would actually see a \"trail\" of the ball as we continuously draw the ball in its new positions.\n",
    "\n",
    "On line 24 we draw the ball image onto the screen. Drawing of images is handled by the Surface.blit() method. A blit basically means copying pixel colors from one image to another. We pass the blit method a source Surface to copy from, and a position to place the source onto the destination.\n",
    "\n",
    "The last thing we need to do is actually update the visible display. Pygame manages the display with a double buffer. When we are finished drawing we call the pygame.display.flip()Update the full display Surface to the screen method. This makes everything we have drawn on the screen Surface become visible. This buffering makes sure we only see completely drawn frames on the screen. Without it, the user would see the half completed parts of the screen as they are being created.\n",
    "\n",
    "That concludes this short introduction to pygame. Pygame also has modules to do things like input handling for the keyboard, mouse, and joystick. It can mix audio and decode streaming music. With the Surfaces you can draw simple shapes, rotate and scale the picture, and even manipulate the pixels of an image in realtime as numpy arrays. Pygame also has the ability to act as a cross platform display layer for PyOpenGL. Most of the pygame modules are written in C, few are actually done in Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
